package com.zklh.leetcode.interviewTop150;

import com.zklh.leetcode.tree.model.ListNode;

public class Solution82 {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode prev = new ListNode(101);
        ListNode headPrev = prev;
        prev.next = head;
        ListNode current = head;
        while (current != null) {
            if (current.next != null && current.next.val == current.val) {
                int repeatVal = current.val;
                current = current.next.next;
                while (current != null && current.val == repeatVal) {
                    current = current.next;
                }
                prev.next = current;
            } else {
                prev = current;
                current = current.next;
            }
        }
        return headPrev.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next.next = new ListNode(5);

        Solution82 solution82 = new Solution82();
        System.out.println(solution82.deleteDuplicates(head));
    }

}
